<template>
  <div class="app-container">
    <!-- 站点选择 -->
    <el-card
      class="site-list-container"
      body-style="padding-left: 10px; padding-right: 10px"
    >
      <div class="site-list-wrapper">
        <el-input
          class="site-search"
          v-model="keywords"
          size="small"
          placeholder="请输入关键字查询"
          clearable
        >
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <div class="site-list">
          <template v-for="(item, index) in siteList">
            <div
              v-if="item.siteName.indexOf(keywords) !== -1"
              class="site-btn"
              :class="{ selected: curSiteId === item.id }"
              size="small"
              :key="index"
              @click="curSiteId = curSiteId === item.id ? '' : item.id"
            >
              {{ item.siteName }}
            </div>
          </template>
        </div>
      </div>
    </el-card>

    <!-- 设备table -->
    <el-card class="station-table-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="站点名称" prop="stationName">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入站点名称"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-download" size="mini" @click="exportData"
            >导出</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="站点名称" prop="name" />
        <el-table-column label="用户数" prop="userCount" />
        <el-table-column label="站点收入(元)" prop="amount">
          <template slot-scope="scope">
            <span>{{ scope.row.amount / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="实体卡收入(元)" prop="orderAmountCard">
          <template slot-scope="scope">
            <span>{{ scope.row.orderAmountCard / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充电收入(元)" prop="orderAmount">
          <template slot-scope="scope">
            <span>{{ scope.row.orderAmount / 100 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="充电次数(元)" prop="chargeTimesCount" />
        <el-table-column label="用电量(度)" prop="chargeKWH">
          <template slot-scope="scope">
            <span>{{ scope.row.chargeKWH / 1000 }}</span>
          </template>
        </el-table-column>

        <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
          <template slot-scope="scope" v-if="scope.row.roleId !== 1">
            <el-button size="mini" type="text" icon="el-icon-search" @click="toDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column> -->
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[10, 20, 30, 50, 500, 1000]"
      />
    </el-card>
  </div>
</template>

<script>
import { getListAPI as getSiteListAPI } from "@/api/charge/site";
import { getListAPI, deleteAPI } from "@/api/charge/station";
import { getSiteAnalysisAPI } from "@/api/charge/siteAnalysis";
import { exportExcel, exportExcelFromAPI } from "@/utils/utils";

export default {
  name: "siteAnalysis",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      tableList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      editVisible: false,

      // 日期范围
      dateRange: [],

      // 站点搜索关键词
      keywords: "",
      curSiteId: "",

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
      },
      // 表单参数
      form: {},

      // 设备收费模式
      chargeForm: {},

      // 全部站点列表
      siteList: [],

      stationData: {},
    };
  },
  created() {
    this.getSiteList();
    this.getList();
  },
  watch: {
    curSiteId() {
      this.getList();
    },
  },
  methods: {
    exportData() {
      exportExcelFromAPI("/statistics/data/listStationStatistics/export", {
        ...this.queryParams,
        pageNum: 1,
        pageSize: 10000,
      });
      return false;
      exportExcel({
        data: this.formatTableList(this.tableList),
        headers: [
          { label: "站点名称", key: "name" },
          { label: "用户数", key: "userCount" },
          { label: "站点收入(元)", key: "amount" },
          { label: "实体卡收入(元)", key: "orderAmountCard" },
          { label: "充电收入(元)", key: "orderAmount" },
          { label: "充电次数", key: "chargeTimesCount" },
          { label: "用电量(度)", key: "chargeKWH" },
        ],
        filename: "站点统计数据.xlsx",
      });
    },
    // 格式化表格数据
    formatTableList(data) {
      return data.map((item) => {
        return {
          ...item,
          chargeKWH: item.chargeKWH / 1000,
          amount: item.amount / 100,
          orderAmount: item.orderAmount / 100,
          orderAmountCard: item.orderAmountCard / 100,
        };
      });
    },
    // 查询全部站点
    getSiteList() {
      getSiteListAPI({
        pageNum: "1",
        pageSize: "500",
      }).then((res) => {
        this.siteList = res.rows;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      getSiteAnalysisAPI({
        ...this.queryParams,
        siteId: this.curSiteId,
      }).then((res) => {
        this.tableList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 跳转详情页 */
    toDetail(row) {},
  },
};
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  display: flex;

  .site-list-container {
    width: 20%;
    flex: none;
    margin-right: 20px;

    .site-list-wrapper {
      position: relative;
      overflow: hidden;

      .site-search {
        position: absolute;
        top: 0;
        margin-bottom: 20px;
      }

      .site-list {
        margin-top: 40px;
        overflow: auto;

        .site-btn {
          width: 100%;
          font-size: 12px;
          color: #606266;
          padding: 9px 15px;
          text-align: left;
          border-radius: 2px;
          cursor: pointer;

          &:hover {
            color: #1890ff;
            background-color: #e8f4ff;
          }

          &.selected {
            color: #1890ff;
            background-color: #e8f4ff;
          }
        }
      }
    }
  }

  .station-table-container {
    width: 80%;
    flex: auto;
  }
}
</style>
